package game

import (
	"data/config"
	"data/view"
	"public/message"
	"public/mycache"
	"public/mysqldb"
	"public/tools"

	"github.com/ant0ine/go-json-rest/rest"
)

//保存游戏信息
func SaveGameInfo(w rest.ResponseWriter, r *rest.Request) {
	var req SaveGameInfoParam
	tools.GetRequestJsonObj(r, &req)

	//入口参数检测
	if !tools.CheckParam(req.Open_id) || req.Pass_count == 0 || req.Game_time == 0 {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid))
		return
	}
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var game view.Game_pass_tbl
	game.Open_id = req.Open_id
	game.Pass_count = req.Pass_count
	game.Game_time = req.Game_time
	err := orm.Create(&game).Error
	if err != nil {
		w.WriteJson(message.GetErrorMsg(message.UnknownError, "重复游戏"))
		return
	}
	w.WriteJson(message.GetSuccessMsg())

}

//获取游戏信息
func GetGameInfo(w rest.ResponseWriter, r *rest.Request) {
	var req GetGameInfoParam
	tools.GetRequestJsonObj(r, &req)

	//入口参数检测
	if !tools.CheckParam(req.Random_code) {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid))
		return
	}
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var game view.Game_pass_tbl
	var user view.User_wx_info_tbl
	//先从缓存中获取
	cache := mycache.OnGetCache("scan")
	var tp interface{}
	tp, b := cache.Value(req.Random_code)
	if b {
		user = tp.(view.User_wx_info_tbl)
		orm.Where("open_id = ?", user.Open_id).Find(&game)
	} else {
		w.WriteJson(message.GetErrorMsg(message.NotFindError))
		return
	}
	mp := make(map[string]interface{})
	mp["user"] = user
	mp["game"] = game
	msg := message.GetSuccessMsg()
	msg.Data = mp
	w.WriteJson(msg)
}

//获取排名信息
func GetRank(w rest.ResponseWriter, r *rest.Request) {
	var req GetRankParam
	tools.GetRequestJsonObj(r, &req)

	//入口参数检测
	if !tools.CheckParam(req.Open_id) {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid))
		return
	}
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var tmp, ranks []view.Game_pass_view
	var more []view.Game_pass_tbl
	var game view.Game_pass_view

	orm.Table("game_pass_tbl as g").Joins("join user_wx_info_tbl as u on g.open_id = u.open_id").Select("g.*,u.*").
		Order("pass_count DESC,game_time ASC").Offset(0).Limit(config.GetRank_count()).Scan(&tmp)

	if len(tmp) > 0 {
		for k, v := range tmp {
			var t view.Game_pass_view
			t.Open_id = v.Open_id
			t.Nick_name = v.Nick_name
			t.Sex = v.Sex
			t.Province = v.Province
			t.City = v.City
			t.Country = v.Country
			t.Headimg_url = v.Headimg_url
			t.Game_time = v.Game_time
			t.Pass_count = v.Pass_count
			t.Rank = k + 1
			if k > 0 {
				pre := ranks[k-1]
				if v.Pass_count == pre.Pass_count && v.Game_time == pre.Game_time {
					t.Rank = pre.Rank
				}
			}
			ranks = append(ranks, t)

		}
	}
	orm.Table("game_pass_tbl as g").Joins("join user_wx_info_tbl as u on g.open_id = u.open_id").Select("g.*,u.*").
		Where("g.open_id = ?", req.Open_id).Find(&game)

	//排名比我靠前的
	orm.Where("pass_count > ?", game.Pass_count).Or("pass_count = ? and game_time < ?", game.Pass_count, game.Game_time).
		Find(&more)
	mine := len(more) + 1
	if len(more) == 0 {
		mine = 1
	}

	mp := make(map[string]interface{})
	mp["ranks"] = ranks
	mp["mine"] = mine
	mp["mine_info"] = game
	msg := message.GetSuccessMsg()
	msg.Data = mp
	w.WriteJson(msg)

}
